<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryForm" :inline="true">
        <!-- StartDate  EndDate -->
        <el-form-item prop="keywords">
          <el-input v-model="queryForm.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-button v-hasPerm="['sys:appcheckinauth:add']" type="primary" size="default" @click="handleAdd()"
            >新增</el-button
          >
        </el-col>
        <el-col :span="24" :offset="0" style="margin-top: 10px">
          <el-table
            ref="tableRef"
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            :show-overflow-tooltip="{ effect: 'light' }"
          >
            <el-table-column label="序号" type="index" width="60" align="center"></el-table-column>
            <el-table-column label="所属项目" prop="projectName" min-width="250" />
            <el-table-column label="所属企业" prop="corpName" min-width="200" />
            <el-table-column label="状态" prop="isDisable" min-width="80">
              <template #default="scope">
                <span v-if="scope.row.isDisable">
                  <el-text type="warning"> 禁用</el-text>
                </span>
                <span v-else>
                  <el-text type="success"> 启用 </el-text>
                </span>
              </template>
            </el-table-column>
            <el-table-column label="开始日期" prop="startDate" min-width="100" />
            <el-table-column label="结束日期" prop="endDate" min-width="80" />
            <el-table-column label="创建时间" prop="createTime" min-width="200" />
            <el-table-column label="最后修改时间" prop="lastUpdateTime" min-width="120" />
            <el-table-column label="操作" fixed="right" max-width="260" min-width="160">
              <template #default="scope">
                <div v-hasData="scope.row.hasEdit" style="display: inline">
                  <el-button
                    v-if="scope.row.isDisable"
                    v-hasPerm="['sys:appcheckinauth:enable']"
                    type="primary"
                    link
                    size="small"
                    @click="handleEnable(scope.row)"
                  >
                    启用
                  </el-button>
                  <el-button
                    v-else
                    v-hasPerm="['sys:appcheckinauth:disable']"
                    type="primary"
                    link
                    size="small"
                    @click="handleDisable(scope.row)"
                  >
                    禁用
                  </el-button>
                  <el-button
                    v-hasPerm="['sys:appcheckinauth:delete']"
                    type="primary"
                    link
                    size="small"
                    @click="handelDelete(scope.row)"
                  >
                    删除
                  </el-button>
                  <el-button
                    v-hasPerm="['sys:appcheckinauth:edit']"
                    type="primary"
                    link
                    size="small"
                    @click="handelUpdate(scope.row)"
                  >
                    修改
                  </el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryForm.pageNum"
            v-model:limit="queryForm.pageSize"
            @pagination="handleQuery"
          />
        </el-col>
      </el-row>
    </div>
    <add-form v-if="dialogAddOrEdit.visible" :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose" />
  </div>
</template>
<script setup lang="ts">
import { ElMessageBox } from "element-plus";
import addForm from "./addForm.vue";
import {
  QueryAppCheckinAuthList,
  DeleteAppCheckinAuth,
  EnableAppCheckinAuth,
  DisableAppCheckinAuth,
} from "@/api/device/index";

const loading = ref(false);
const tableData = ref();

const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
});
const tableRef = ref();

//新增
const handleAdd = function () {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "提交";
  dialogAddOrEdit.title = "创建";
};
//修改
const handelUpdate = function (row: any) {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "确认修改";
  dialogAddOrEdit.title = "修改";
  dialogAddOrEdit.id = row.id;
};
//详情
const handelDetail = function (row: any) {
  dialogDetail.visible = true;
  dialogDetail.id = row.id;
  dialogDetail.title = "详情";
  dialogDetail.type = 0;
};

//删除
const handelDelete = function (row: any) {
  ElMessageBox.confirm("确认删除吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    loading.value = true;
    const res = await DeleteAppCheckinAuth(row.id);
    if (res.type === 0) {
      if (res.success) {
        ElMessage.success(res.msg);
        handleQuery();
      } else {
      }
    } else {
    }
    loading.value = false;
  });
};

//启用
const handleEnable = function (row: any) {
  ElMessageBox.confirm("确认启用吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    loading.value = true;
    const res = await EnableAppCheckinAuth(row.id);
    if (res.type === 0) {
      if (res.success) {
        ElMessage.success(res.msg);
        handleQuery();
      } else {
      }
    } else {
    }
    loading.value = false;
  });
};
//禁用
const handleDisable = function (row: any) {
  ElMessageBox.confirm("确认禁用吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    loading.value = true;
    const res = await DisableAppCheckinAuth(row.id);
    if (res.type === 0) {
      if (res.success) {
        ElMessage.success(res.msg);
        handleQuery();
      } else {
      }
    } else {
    }
    loading.value = false;
  });
};

const sendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  if (needRefresh) {
    handleQuery();
  }
};

//查询条件
const queryForm = reactive({
  keywords: "",
  pageNum: 1,
  pageSize: 10,
});
const total = ref(0);

//这是列表数据
const handleQuery = function () {
  loading.value = true;
  QueryAppCheckinAuthList(queryForm)
    .then((res) => {
      if (res.type === 0) {
        if (res.success) {
          tableData.value = res.data.list;
          queryForm.pageNum = res.data.pageNum;
          queryForm.pageSize = res.data.pageSize;
          total.value = res.data.total;
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>
<style scoped>
:deep(.el-button) {
  margin-left: 5px;
}
:deep(.el-input__wrapper) {
  border-radius: 0;
}
</style>
